<template>
  <div class="article-container">
    <!-- 导航栏 -->
    <van-nav-bar
      fixed
      left-arrow
      @click-left="$router.back()"
      title="文章详情"
    ></van-nav-bar>

    <!-- 加载中 loading -->
    <van-loading
      v-if="loading"
      class="article-loading"
    />

    <!-- 404页面 -->
    <div
      class="error"
      v-if="is404"
    >
      <p>文章被外星人吃掉了</p>
      <van-button @click="$router.back()">后退</van-button>
      <van-button @click="$router.push('/')">回主页</van-button>
    </div>

    <!-- 文章详情 -->
    <div
      class="detail"
      v-else
    >
      <h3 class="title">{{ article.title }}</h3>
      <!-- 引入封装好author.vue -->
      <author :author="article">
        <van-button
          @click="hSwitchFollow"
          round
          size="small"
          type="info"
        >{{ article.is_followed ? '取关' : '+ 关注'}}</van-button>
      </author>
      <div class="content">
        <div v-html="article.content"></div>
      </div>
      <van-divider>END</van-divider>
      <div class="zan">
        <van-button
          round
          size="small"
          hairline
          type="primary"
          plain
          :icon="article.attitude === 1 ? 'good-job' : 'good-job-o'"
          @click="hSwitchLike"
        >{{ article.attitude == 1 ? "取消点赞" : "点赞" }}</van-button>
        &nbsp;&nbsp;&nbsp;&nbsp;
        <van-button
          round
          size="small"
          hairline
          type="danger"
          plain
          icon="delete"
          @click="hSwitchDisLike"
        >{{article.attitude === 0 ? '取消不喜欢' : '不喜欢'}}</van-button>
      </div>
    </div>
    <!-- 文章评论 -->
    <article-comment :articleId="articleId"></article-comment>
  </div>
</template>

<script>
import Author from '@/components/author'
import { getDetail, addLike, deleteLike, addDisLike, deleteDisLike } from '@/api/article.js'
import { followed, unFollowed } from '@/api/user.js'
import ArticleComment from './comment'
export default {
  components: {
    Author,
    ArticleComment
  },
  name: 'ArticleIndex',
  data () {
    return {
      articleId: '', // 文章id
      is404: false, // 文章是否存在
      loading: true, // 控制加载中的 loading 状态
      article: {} // 文章详情
    }
  },
  created () {
    // 保存文章id
    this.articleId = this.$route.params.id
    this.loadDetail()
  },
  watch: {
    '$route.params.id' (newValue, oldValue) {
      console.log(newValue, '-----', oldValue)
      // 重发请求
      this.loadDetail()
    }
  },
  methods: {
    // 不喜欢或取消不喜欢
    async hSwitchDisLike () {
      // 如果attitude是0即 不喜欢，则 取消不喜欢
      if (this.article.attitude === 0) {
        await deleteDisLike(this.article.art_id.toString())
        // 更新状态
        this.article.attitude = -1
      } else {
        await addDisLike(this.article.art_id.toString())
        // 更新状态
        this.article.attitude = 0
      }
      this.$toast.success('操作成功')
    },
    // 点赞或取消点赞
    async hSwitchLike () {
      // 点赞状态取消点赞
      if (this.article.attitude === 1) {
        await deleteLike(this.article.art_id.toString())
        // 更新状态
        this.article.attitude = -1
      } else {
        await addLike(this.article.art_id.toString())
        // 更新状态
        this.article.attitude = 1
      }
      this.$toast.success('操作成功')
      // this.article.attitude = this.article.attitude * -1
    },
    // 关注或取关
    async hSwitchFollow () {
      const { is_followed: isFollowed, aut_id: autId } = this.article
      try {
        if (isFollowed) {
          // true 取消关注
          const res = await unFollowed(autId)
          console.log(res)
          this.$toast.success('取关成功')
          // 修改页面的状态
          this.article.is_followed = false
        } else {
          // false 关注
          const res = await followed(autId)
          console.log(res)
          this.$toast.success('加关成功')
          // 修改页面的状态
          this.article.is_followed = true
        }
      } catch (err) {
        this.$toast.fail('操作失败')
      }
    },
    // 获取文章详情
    async loadDetail () {
      try {
        this.is404 = false
        // 1 发送请求获取文章详情
        const res = await getDetail(this.$route.params.id)
        // 2 保存文章详情
        this.article = res.data.data
        // 3 关闭加载
        this.loading = false
      } catch (err) {
        this.loading = false
        console.log(err)
        // 如何去判断本次请求是404?
        if (err.response.status === 404) {
          this.is404 = true
        }
      }
    }
  }
}
</script>

<style scoped lang='less'>
.article-container {
  position: absolute;
  left: 0;
  top: 0;
  overflow-y: scroll;
  width: 100%;
  height: 100%;
}
.article-loading {
  padding-top: 100px;
  text-align: center;
}
.error {
  padding-top: 100px;
  text-align: center;
}
.detail {
  padding: 50px 10px;
  .title {
    font-size: 16px;
  }
  .zan {
    text-align: center;
  }
  .author {
    padding: 10px 0;
    display: flex;
    .text {
      flex: 1;
      padding-left: 10px;
      line-height: 1.3;
      .name {
        font-size: 14px;
        margin: 0;
      }
      .time {
        margin: 0;
        font-size: 12px;
        color: #999;
      }
    }
  }
  .content {
    font-size: 14px;
    overflow: hidden;
    white-space: pre-wrap;
    word-break: break-all;
    /deep/ img {
      max-width: 100%;
      background: #f9f9f9;
    }
  }
}
/deep/ .van-nav-bar .van-icon {
  color: #fff;
}
</style>
